<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category RoleService.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/6  17:59
 * +--------------------------------------------------------+
 */

namespace App\Services;

use App\Exceptions\SystemException;
use App\Repositories\PermissionRepository;
use App\Repositories\RoleRepository;

class RoleService extends BaseService
{
    protected $roleRepository;
    protected $permissionRepository;

    public function __construct(RoleRepository $roleRepository, PermissionRepository $permissionRepository)
    {
        $this->roleRepository       = $roleRepository;
        $this->permissionRepository = $permissionRepository;
    }

    /**
     * @param $params
     * @return array
     * @Depiction 列表
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  13:57
     */
    public function getList($params)
    {
        $data = $this->roleRepository->getList($params);

        return $this->initPaginateData($data);
    }

    /**
     * @param $id
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  14:15
     */
    public function getInfoWithPermission($id)
    {
        $role = $this->roleRepository->getInfo($id);

        $permissions = $role->permissions;
        $role->users;

        $permissionIdList = $permissions->mapWithKeys(function ($permission, $key) {
            return [$key => $permission->id];
        });

        $role->permissionIdList = $permissionIdList;

        return $role;

    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:17
     */
    public function getInfo($id)
    {
        return $role = $this->roleRepository->getInfo($id);

    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:54
     */
    public function getInfoByName($params)
    {
        return $this->roleRepository->getInfoByName($params['name'], $params['guard_name']);

    }

    /**
     * @param $params
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  14:31
     */
    public function getAllList($params): array
    {
        $data = $this->roleRepository->getAllList($params);

        return $data->toArray();
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:18
     */
    public function create($params)
    {
        return $res = $this->roleRepository->create($params);
    }

    /**
     * @param $role
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:23
     */
    public function update($role, $params)
    {
        return $res = $this->roleRepository->update($role, $params);
    }

    /**
     * @param $role
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:24
     */
    public function delete($role)
    {
        return $res = $this->roleRepository->delete($role);
    }

    /**
     * @param $role
     * @param $params
     * @return bool
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/13  18:07
     */
    public function syncPermissions($id, $params)
    {
        $role = $this->getInfo($id);
        if (empty($role)) {
            throw new SystemException('角色不存在！');
        }

        $permissions = isset($params['permissions']) ?
            $this->permissionRepository->getAllInId($params['permissions']) :
            [];

        $res = $role->syncPermissions($permissions);

        return true;
    }
}
